var express = require('express');
var userModule = require('../model/userMode');
var router = express.Router();
// 用户 =》 注册接口
/* 
    {
        code:0,
        msg:'注册成功'
    }
    {
        code:1,
        msg:'用户名密码不能为空'
    }
    {
        code:2,
        msg:'两次密码不一样'
    }
    {
        code:3,
        msg:'用户名已存在'
    }

    {code: 4, msg: '验证码不正确'}
    {code: 5, msg: '请填写验证码'}

*/
router.post('/regist', (req, res) => {
    let { username, password, repassword ,yzm} = req.body;
    yzm = yzm.toLowerCase()  //验证码，转小写
     // 可以对用户名/密码格式进行检查, 如果非法, 返回提示信息
    let reqyzm = req.session.yzm ? req.session.yzm.toLowerCase():'';
    if(yzm!==reqyzm) {
        return res.send({code: 4, msg: '验证码不正确'});
    }
    if(yzm=='') {
        return res.send({code: 5, msg: '请填写验证码'});
    }
    // 验证通过，删除保存的验证码
    delete req.session.yzm;
    if (username == '' || password == '' || repassword == '') {
        res.json({
            code: 1,
            msg: '用户名和密码不能为空'
        });
        return;
    };
    if (repassword != password) {
        res.json({
            code: 2,
            msg: '两次密码不一样'
        });
        return;
    }
    // 数据库查找
    // userModule.create()
    userModule.findOne({ username: username }).then((data) => {
        console.log(data);//对象
        if (!data) {
            let obj = { ...req.body};
            delete obj.repassword;
            userModule.create({...obj})
            .then(() => {
                res.send({
                    code: 0,
                    msg: '注册成功'
                })
            })
        } else {
            res.json({
                code: 3,
                msg: '用户名已存在'
            })
        }
    })
});
// 验证用户名
router.post('/regist/get/user',(req,res)=>{
    userModule.findOne({username:req.body.username}).then((data)=>{
        if (!data) {
            return res.send({code:0,msg:'可以注册'});
        }else{
            return res.send({code:1,msg:'用户存在'});
        }
    })
})
/* 
    成功
{
    code: 0,
    token:{
        isAdmin:true/false,
        username:xxx,
        uid:id
    }
}
用户名或密码不能为空
{
    code: 1,
    msg:'用户名或密码不能为空'
}

用户名不存在
{
    code: 2,
    msg:'用户名不存在'
}
密码错误
{
    code: 3,
    msg:'密码错误'
}


*/
// 登录接口
router.post('/login', (req, res) => {
    let { username , password } = req.body;
    console.log(req.body);
    if(username=='' || password ==''){
        res.send({
            code: 1,
            msg:'用户名或密码不能为空'
        })
    }
    userModule.findOne({username})
    .then((obj)=>{
        console.log(obj)
        if(obj ==null){
            res.json({
                code: 2,
                msg:'用户名不存在'
            })
        }else {
            if(obj.username == username && obj.password == password){
                // console.log(obj._id.toString());
                // let _id = obj._id.toString();
                // let jwt = new JwtUtil(_id);
                // let token = jwt.generateToken();
                // console.log(token)
                res.send({
                    code: 0,
                    token:{
                        isAdmin:obj.isAdmin,
                        username:obj.username,
                        uid:obj._id
                    }
                })
                return;
            }
            if(obj.username == username && obj.password != password){
                res.send({
                    code: 3,
                    msg:'密码错误'
                })
            }
        }
    })
    
    
})
module.exports = router;